import React, {useEffect} from 'react';
import {Modal, Form, Input, Button} from 'antd';
import {formTextAreaRules} from '@/utils/rules';
import {handleSuggestionEmergency} from '@/services/emergency';
import {useRequest} from "ahooks";
import {filterEmpty} from '@/utils/methods';

const CaseProcessingModal: React.FC<any> = ({emergencyId, onSuccess, dispatch, ...props}: any) => {
  const [form] = Form.useForm();
  const {
    run: runSubmit,
    loading: loadingSubmit
  } = useRequest((values: any) => {
    const params = filterEmpty({
      originValues: values,
      keepEmpty: true,
    });
    params.emergencyId = emergencyId;
    return handleSuggestionEmergency(params);
  }, {
    manual: true,
    onSuccess(res) {
      dispatch({
        clear: true,
        open: false
      })
      onSuccess && onSuccess(res);
    }
  })

  useEffect(() => {
    props.open || form.resetFields();
  }, [props.open]);
  return (
    <Modal
      {...props}
      getContainer={false}
      onCancel={() => {
        dispatch({
          clear: true,
          open: false
        })
      }}
      footer={[
        <Button key="cancel" onClick={() => {
          dispatch({
            clear: true,
            open: false
          })
        }}>
          关闭
        </Button>,
        <Button
          key="submit"
          type="primary"
          loading={loadingSubmit}
          onClick={async () => {
            const value: any = await form.validateFields()
            runSubmit(value)
          }}
        >
          保存
        </Button>,
      ]}
    >
      <Form form={form} labelCol={{span: 9, offset: 0}}>
        <Form.Item
          name="handleSuggestion"
          label="处理意见"
          labelCol={{span: 4}}
          rules={[
            ...formTextAreaRules,
            {
              required: true,
              message: '请填写处理意见',
            },
          ]}
        >
          <Input.TextArea/>
        </Form.Item>
      </Form>
    </Modal>
  );
}

export default CaseProcessingModal;
